package xddq

import (
	"compress/gzip"
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"
	"strings"
	"time"
	"websocket_apn/log"
	http_transport "websocket_apn/transfer/net/http"
)

type ApiForumSignReq struct {
	GiftType string
	AppPst   string
}
type ApiForumSignRes struct {
	Result int         `json:"result"`
	Msg    string      `json:"msg"`
	Val    interface{} `json:"val"`
	List   interface{} `json:"list"`
}

func ApiSign(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始论坛圈子签到")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://luntan.oiz611.com/api/mobile/index.php?module=gamebox&func=do_exp_task&version=4&mapifrom=wx&charset=utf-8&game_id=913&pf=1&video=1&forbid_share=1&loginout=0&sid=3109673&role_id=3109673861602&user_token=" + req.AppPst
	post := "play_platform=web&app_version=1.0.0&from_type=2&task_type=1&fid=201&pid=&source_t=wxxyx"
	method := "POST"
	head.Add("Host", "luntan.oiz611.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "application/json, text/plain, */*")
	head.Add("Content-Type", "application/x-www-form-urlencoded")
	head.Add("Origin", "https://luntan.oiz611.com")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "empty")
	head.Add("Referer", "https://luntan.oiz611.com/new_m/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiForumSign error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiForumSign Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiForumSign read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiForumSign read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	err = json.Unmarshal(reapData, rspData)
	if err != nil {
		log.Errorf("ApiForumSign Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiForumSign:%+v\n", rspData)
	return rspData
}

/*
GET https://hdapi.37.com/?c=api&a=sign&alias_info=xddqwxqd20240911&f=d202409/xddqwxqd20240911&callback=jQuery1830055365662423174866_1731720886205&game_id=900&app_pst=NGI0ME1CbENHQVVNMW1qMHBReVZHODR2OWowY1MvN1dMOXFyOG4wREp5K2ttZUxPdHBLemhIL3c1cFNUNDFJRGo0NGVPUi9Qa0VIMElNNERFVThOdmRrZng2ZnhMZk0zTWFMY1FHMEZrQUN6S3E2dkYxeHlQS1VsUUcvRytzZnpwTDM5ODJJdVpCTTNlSFZuQ2hHMnZrREh0Z1drSmdCK0ZKcnUzUHFaSCt2UkJrSzUxb2kvOUt4MURoYXFqUDVKMC9tU0xnK0c2V0RocFc2TnVJQURwbXJnZHVYVWhyZDl3UzFSUXU3RA&_=1731720893309 HTTP/1.1
Host: hdapi.37.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477
Accept: */ /*
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: script
Referer: https://activity.37.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: 37wanrefer=wxxyx%7C%7C%7C%7C; PHPSESSID=ans6ji1tg2ls6vg3gb8alfsvq6; tg_uv=t.Y3Z59tJU4BAAAAXlwS

HTTP/1.1 200 OK
Date: Sat, 16 Nov 2024 01:34:53 GMT
Content-Type: text/html;charset=UTF-8
Connection: keep-alive
Server: openresty
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 113

jQuery1830055365662423174866_1731720886205({"result":1,"msg":"\u7b7e\u5230\u6210\u529f","val":null,"list":null});
*/

func ApiForumSign(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始论坛签到")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=api&a=sign&alias_info=xddqwxqd20240911&f=d202409/xddqwxqd20240911&callback=jQuery1830055365662423174866&game_id=900&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiForumSign error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiForumSign Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiForumSign read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiForumSign read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiForumSign regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiForumSign Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiForumSign:%+v\n", rspData)
	return rspData
}

func ApiGetGift(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始领取论坛签到礼物")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=c_get_gift&a=default&alias_info=xddqwxqd20240911&feature_key=get_gift_1&f=auto&callback=jQuery1830055365662423174866_1731720886205&game_id=900&class_key=" + req.GiftType + "&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGift error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGift regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGift Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGift:%+v\n", rspData)
	return rspData
}

func ApiGetGiftStatus(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始查询论坛签到天数")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=api&a=status&alias_info=xddqwxqd20240911&f=d202409/xddqwxqd20240911&callback=jQuery183009287529915540516&game_id=900&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGiftStatus error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGiftStatus Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGiftStatus read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGiftStatus read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGiftStatus regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGiftStatus Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGiftStatus:%+v\n", rspData)
	return rspData
}

//每日摇一摇
func ApiGetShakeGift(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始获取摇一摇礼物")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=c_lottery_normal&a=roll&alias_info=xddqflzx20230814&feature_key=lottery_normal_1&f=auto&game_id=936&role_id=3109673861602&mini_app_pst=&mini_sign=&hd_referer=&callback=__jp12&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGift error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGift regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGift Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGift:%+v\n", rspData)
	return rspData
}

//摇一摇月度礼物
func ApiGetShakeMonthlyGift(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("开始获取摇一摇月度礼物")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=c_lottery_normal&a=roll&alias_info=xddqflzx20230814&feature_key=lottery_normal_1&f=auto&game_id=936&role_id=3109673861602&mini_app_pst=&mini_sign=&hd_referer=&callback=__jp12&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGift error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGift regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGift Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGift:%+v\n", rspData)
	return rspData
}

//生成修仙周记礼包
func ApiCreatBattleWeeklyGift(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("生成修仙周记礼包")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=api&a=battle_status&alias_info=xddqxxzj20230911&f=d202309/xddqxxzj20230911&game_id=900&sid=3109673&role_id=3109673861602&mini_app_pst=&mini_sign=&week=w" + time.Now().Format("20060102") + "&callback=__jp6&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGift error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGift regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGift Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGift:%+v\n", rspData)
	return rspData
}

//获取修仙周记礼包
func ApiGetBattleWeeklyGift(req ApiForumSignReq) *ApiForumSignRes {
	log.Infof("获取修仙周记礼包")
	rspData := &ApiForumSignRes{}
	head := http.Header{}
	url := "https://hdapi.37.com/?c=c_get_gift&a=default&alias_info=xddqxxzj20230911&feature_key=get_gift_1&f=auto&game_id=900&sid=3109673&role_id=3109673861602&mini_app_pst=&mini_sign=&class_key=other_common_5&is_app=0&callback=__jp12&app_pst=" + req.AppPst + "&_=" + fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond))
	post := ""
	method := "GET"
	head.Add("Host", "hdapi.37.com")
	head.Add("Connection", "keep-alive")
	head.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11477")
	head.Add("Accept", "*/*")
	head.Add("Sec-Fetch-Site", "same-site")
	head.Add("Sec-Fetch-Mode", "no-cors")
	head.Add("Sec-Fetch-Dest", "script")
	head.Add("Referer", "https://activity.37.com/")
	head.Add("Accept-Encoding", "gzip, deflate, br")
	head.Add("Accept-Language", "zh-CN,zh;q=0.9")
	c, _ := http_transport.NewClient()
	ctx := context.WithValue(context.Background(), "url", url)
	ctx = context.WithValue(ctx, "method", method)
	ctx = context.WithValue(ctx, "Header", head)
	payload := strings.NewReader(post)
	resp, err := c.RoundTrip(ctx, payload)
	if err != nil {
		log.Errorf("ApiGetGift error: %s", err.Error())
		return nil
	}
	defer resp.Body.Close()
	var reapData []byte
	if resp.Header.Get("Content-Encoding") == "gzip" {
		// 创建一个 gzip 读取器
		gzReader, err := gzip.NewReader(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift Error creating gzip reader:", err)
			return nil
		}
		defer gzReader.Close()
		reapData, err = ioutil.ReadAll(gzReader)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	} else {
		reapData, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Errorf("ApiGetGift read body error: %s\n", err.Error())
			return nil
		}
	}
	//log.Debugf("head:%#v\n", resp.Header)
	//log.Debugf("%s", string(reapData))
	matches := regexp.MustCompile(`\(({.*})\);`).FindStringSubmatch(string(reapData))
	//log.Debugf("%#v", matches)
	if len(matches) < 2 {
		log.Fatal("ApiGetGift regexp body error")
	}
	err = json.Unmarshal([]byte(matches[1]), rspData)
	if err != nil {
		log.Errorf("ApiGetGift Unmarshal body error: %s\n", err.Error())
		return nil
	}
	log.Debugf("ApiGetGift:%+v\n", rspData)
	return rspData
}

func GiftSign(appPst string) {
	currentTime := time.Now()
	req := ApiForumSignReq{AppPst: appPst}
	//论坛圈子签到
	ApiSign(req)
	//论坛礼物签到
	ApiForumSign(req)
	req.GiftType = "otherlimit_common_1"
	//论坛签到日礼物
	ApiGetGift(req)

	//查看礼物签到天数
	res := ApiGetGiftStatus(req)
	giftNeedDays, ok := res.List.(map[string]interface{})["gift_need_days"].([]interface{})
	if ok {
		for idx, days := range giftNeedDays {
			if valF, ok := days.(float64); ok {
				val := int(valF)
				if idx == 1 {
					if val > 1 {
						log.Debugf("论坛再签到%d天可领取周礼物", val)
					} else {
						log.Debugf("开始领取论坛签到周礼物")
						//论坛签到周礼物
						req.GiftType = "otherlimit_common_2"
						ApiGetGift(req)
					}
				} else if idx == 2 {
					if val > 1 {
						log.Debugf("论坛再签到%d天可领取月礼物", val)
					} else {
						log.Debugf("开始领取论坛签到月礼物")
						//论坛签到月礼物
						req.GiftType = "otherlimit_common_3"
						ApiGetGift(req)
					}
				}
			}
		}
	}
	//修仙周记礼物
	if currentTime.Weekday() == time.Monday {
		ApiCreatBattleWeeklyGift(req)
		ApiGetBattleWeeklyGift(req)
	}
	//摇一摇礼物
	ApiGetShakeGift(req)
	//摇一摇月礼物
	if currentTime.Day() == 1 {
		ApiGetShakeMonthlyGift(req)
	}
}
